package com.holly.unit.bpmn.activiti.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.holly.unit.auth.api.pojo.login.LoginUser;
import com.holly.unit.bpmn.activiti.entity.ActivitiZNode;
import com.holly.unit.bpmn.activiti.pojo.ActivitiDepartment;
import com.holly.unit.bpmn.activiti.pojo.ActivitiRole;
import com.holly.unit.bpmn.api.pojo.BpmnUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 接口描述: 流程节点扩展Mapper
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/2/21 15:33
 */
public interface ActivitiZNodeMapper extends BaseMapper<ActivitiZNode> {

    /***
     * 获取所有的用户
     * @return
     */
    @Select("select * from sys_user where del_flag = 'N'")
    List<LoginUser> queryAllUser();

    /***
     * 根据用户名称获取角色
     * @param username 用户名
     * @return
     */
    @Select("select role_id from sys_role where role_id in (select role_id from sys_user_role where user_id = (select user_id from sys_user where account=#{username} and del_flag = 'N')) and del_flag = 'N'")
    List<String> selectRoleByUserName(@Param("username") String username);

    // 根据节点的id查询用户
    @Select("select * from sys_user where account in (select relate_id from act_z_node where node_id = #{nodeId} and type = 1 and del_flag = 'N') and del_flag = 'N'")
    public List<LoginUser> selectUserByNodeId(@Param("nodeId") String nodeId);

    // 根据节点的id查询角色
    @Select("select r.* from sys_role r JOIN act_z_node n ON r.role_id = n.relate_id and n.del_flag = 'N' where n.node_id = #{nodeId} and n.type = 0 and r.del_flag = 'N'")
    public List<ActivitiRole> selectRoleByNodeId(@Param("nodeId") String nodeId);

    // 根据节点的id查询机构
    @Select("select * from hr_organization where org_id in (select relate_id from act_z_node where node_id = #{nodeId} and type = 2 and del_flag = 'N') and del_flag = 'N'")
    public List<ActivitiDepartment> selectDepartmentByNodeId(@Param("nodeId") String nodeId);

    // 根据角色id查询用户信息
    @Select("select * from sys_user where user_id in (select user_id from sys_user_role where role_id = #{id}) and del_flag = 'N'")
    public List<LoginUser> selectUserByRoleId(@Param("id") String id);

    // 根据机构id查询用户信息
    @Select("select * from sys_user where user_id in (select user_id from sys_user_org where org_id = #{id}) and del_flag = 'N'")
    List<LoginUser> selectUserDepartmentId(@Param("id") String id);
    // 根据账户信息获取机构ids
    @Select("select org_id from sys_user_org where user_id in (select user_id from sys_user where account=#{account} and del_flag = 'N')")
    List<String> getDepartIdsByUsername(@Param("account") String account);
    // 根据账户信息 获取系统用户的信息
    @Select("select * from sys_user where account='${account}' and del_flag = 'N'")
    BpmnUser getUserByAccount(@Param("account") String account);


}
